var _m_url = window.parent._index_path;

//点击按钮时
function butnChange(obj){
	if($(obj).attr('checked') == "checked"){
		$($(obj).parents('td').prev().find('input')[0]).attr('checked','checked');					 			//功能选中
		$($($($(obj).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('checked','checked');			//模块选中
	}
}
//点击功能时
function gnChange(obj){
	if($(obj).attr('checked') == "checked"){
		$($($($(obj).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('checked','checked');           //模块选中
	}else{
		$($($(obj).parents('tr')[0]).find('input')).attr('checked',false);										//取消所有的按钮
	}
}
//点击模块时
function modChange(obj){
	if($(obj).attr('checked')=='checked'){
		
	}else{
		$($($(obj).parents('table')[0]).find('input')).attr('checked',false);									//取消所有的功能和按钮
//		$($(obj).parents('table').find('input')).attr('checked',false);
	}
}

//初始化界面加载用户
function loadUserCol(){
	alert(3);
	$('#caq_USERLIST').datagrid({
		iconCls:'icon-save',
		url:_m_url+'framework/loadData?dsId=ds_bs_operators&config=&rows=1000',
		height:600 ,
		width:260, 
		striped: true,		
		collapsible:true,
		autoRowHeight: false,		
		sortOrder: 'desc',
		remoteSort: false,
		columns:[[
          {field:'kk',checkbox:true}, 
          {field:"O_ID",title:'员工编码',hidden:true},
          {field:"O_NAME",title:'员工名称',width:100,sortable:true},
          {field:"BUMEN",title:'所属部门',formatter:eval('window.parent.dz_bumenfmat'),width:100,sortable:true}
          ]],
		singleSelect:true,
		onSelect:function(rowIndex,rowData){
			var oid = rowData.O_ID;
			//加载这个人所拥有的所有模块以及菜单
			$.ajax({
				type:'post',
				url:_m_url+'menu/listByUser',
				data:{'oid':oid},
				success:function(data){
					showDiv(data);
					var quBut = $('#seacQu');
					searchMGB(quBut,'0');
				}
			})
		}
	});   
}
//用户点击时
function showDiv(data){
	$('#cat_QueryTaskList input').each(function(){
		$(this).attr('checked',false);
	});
	var haveGroup = data.haveGroup; 																		     //已有的组
    var haveModual = data.haveModual; 																		     //已有的模块
    var glist = data.dataStroeList; 																		     //已有的功能
    var blist = data.butnList;																			         //已有的按钮
    if(haveGroup != null){
    	for(var i = 0;i<haveGroup.length;i++){
			$("#d_uid_19").find('input[gid = '+haveGroup[i].GROUP_ID+']').attr("checked","checked");
		}
    }
    selcFunc('allUserPerms',haveModual,glist,blist);
}
//返回数据的选中数据的方法
function selcFunc(divName,mlist,glist,blist){
	 if(mlist != null){
    	 $('#'+divName).find('table').find('input[mid]').map(function(){
    			for(var i=0;i<mlist.length;i++){
    				if(mlist[i].MODUAL_ID == $(this).attr("mid") ){
    					$(this).attr('checked','checked');														   //模块选中
    				} 
    			}
    		});
	}
	if(glist != null){
		$('#'+divName).find('table').find('input[funcId]').map(function(){
			var modId = $($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('mid');			//模块Id
			for(var j=0;j<glist.length;j++){
				if(glist[j].FUNC_ID == $(this).attr('funcId') && glist[j].MODUAL_ID == modId){
					$($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('checked','checked');	//模块选中
					$(this).attr('checked','checked');																//功能选中
				}
			}
		});
	}
	if(blist != null){
		$('#'+divName).find('table').find('input[butnCode]').map(function(){
			var modId = $($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('mid'); 			//模块Id
			var funcId = $($(this).parents('td').prev().find('input')[0]).attr('funcId');                 			//功能Id
			for(var j=0;j<blist.length;j++){
				if(blist[j].BUTN_CODE == $(this).attr('butnCode') && blist[j].BUTN_ID ==$(this).attr('butnId') && blist[j].FUNC_ID == funcId ){
					$($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('checked','checked');  //模块选中
					$($(this).parents('td').prev().find('input')[0]).attr('checked','checked');						//功能选中
					$(this).attr('checked','checked');																//按钮选中
				}
			}
		})
	}
}


//保存用户所属角色组
function saveUserGroup(){
	var selRows = $('#caq_USERLIST').datagrid('getSelected');
	if(selRows == null){
		$.messager.alert("提示","员工没有选择，请选择员工之后再添加！");
		return;
	}
	var oid= selRows.O_ID; 	
	var objs = $('#d_uid_19 input');
//	var gIds = "";
	var gIds = new Array();
	for(var i=0;i<objs.length;i++){ 																			   //组
		if($(objs[i]).attr('checked')=='checked'){
			gIds.push($(objs[i]).attr('gid'));
		}
	} 
	$.ajax({
		type:'post',
		url:_m_url+'menu/saveGroup?oid='+oid,
		contentType: "application/json;charset=utf-8",
		dataType: "json",
		data:JSON.stringify(gIds),
		success:function(data){
			if(data ){
				$.messager.alert("提示",'保存成功！');
			}else{
				$.messager.alert("提示",'保存失败！');
			}
		}
	})
}

//保存用户所有的权限(模块，功能，按钮)
function saveRightuser(){
	var selRows = $('#caq_USERLIST').datagrid('getSelected');
	if(selRows == null){
		$.messager.alert("提示","员工没有选择，请选择员工之后再添加！");
		return;
	}
	var oid= selRows.O_ID; 																						   //获取选择的员工ID
	var allList = {};
	var mList = new Array();																					   //获取到所有选中模块的模块Id
	var gList = new Array();
	var bList = new Array();																				   //获取到所有选中模块的模块Id
	$('#allUserPerms').find('table').find('input[mid]').map(function(){
		if($(this).attr('checked')=="checked"){
			mList.push($(this).attr('mid'));
		}
	});
	$('#allUserPerms').find('table').find('input[funcId]').map(function(){
		if($(this).attr('checked')=="checked"){
			var gmfList = new Array();
			var modId = $($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('mid');			//模块Id
			gmfList[0]=modId;
			gmfList[1]=$(this).attr('funcId');
			gList.push(gmfList);
		}
	});
	$('#allUserPerms').find('table').find('input[butnCode]').map(function(){
		if($(this).attr('checked')=="checked"){
			var gmfbList = new Array();
			var modId = $($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('mid'); 			 //模块Id
			var funcId = $($(this).parents('td').prev().find('input')[0]).attr('funcId');                 			 //功能Id
			gmfbList[0]=modId;
			gmfbList[1]=funcId;
			gmfbList[2]=$(this).attr('butnId');
			gmfbList[3]=$(this).attr('butnCode');
			gmfbList[4]=$(this).attr('comm');
			bList.push(gmfbList);
		}
	});
	allList.mList = mList;
	allList.gList = gList;
	allList.bList = bList;
	allList.flagStr = '1';
	$.ajax({
		type:'post',
		url:_m_url+'menu/saveUserPerms?oid='+oid,
		contentType: "application/json;charset=utf-8",
		dataType: "json",
		data:JSON.stringify(allList),
		success:function(data){
			if(data){
				$.messager.alert('提示',"保存成功！");
			}else{
				$.messager.alert('提示','保存失败！');
			}
		}
	})
}
//全选或者取消功能的checkbox
function chengeAllFuncU(obj){
	if($(obj).attr('checked')=='checked'){
		$($($(obj).parents('table')[0]).find('input')).attr('checked','checked');
	}else{
		$($($(obj).parents('table')[0]).find('input')).attr('checked',false);
	}
}
//选项卡选中用户角色时执行的方法
function userPerms(){
	$('#allUserPerms input').each(function(){
		$(this).attr('checked',false);
	});
	var selRows = $('#caq_USERLIST').datagrid('getSelected');
	if(selRows == null){
		return;
	}else{
		var oid= selRows.O_ID; 	
		$.ajax({
			type:'post',
			url:_m_url+'menu/permsByUser',
			data:{'oid':oid},
			success:function(data){
				 var haveModual = data.haveModual; 																		     //已有的模块
				 var glist = data.dataStroeList; 																		     //已有的功能
				 var blist = data.butnList;
				 selcFunc('allUserPerms',haveModual,glist,blist);
			}
		})
	}
}
//选项卡选中所属角色组时执行的方法
function userGroupPerms(){
	var selRows = $('#caq_USERLIST').datagrid('getSelected');
	if(selRows == null){
		return;
	}else{
		var oid= selRows.O_ID; 
		$.ajax({
			type:'post',
			url:_m_url+'menu/groupByUser',
			data:{'oid':oid},
			success:function(data){
				var haveGroup = data.haveGroup; 																		     //已有的组
				if(haveGroup != null){
			    	for(var i = 0;i<haveGroup.length;i++){
						$("#d_uid_19").find('input[gid = '+haveGroup[i].GROUP_ID+']').attr("checked","checked");
					}
			    }
			}
		})
	}
}
//字点击的方法
function checkboxUSel(obj,num){
	var objPre= $(obj).prev();
	if($(objPre).attr('checked')=='checked'){
		$(objPre).attr('checked',false);
	}else{
		$(objPre).attr('checked','checked');
	}
	if(num == 0){ 									//模块的选中
		modChange(objPre);
	}else if(num == 1){								//全选和取消的选中
		chengeAllFuncU(objPre);
	}else if(num == 2){								//功能的选中
		gnChange(objPre);
	}else if(num == 3){								//按钮的选中
		butnChange(objPre);
	}
//	else if(num == 4){								//组的选中
//		groupChange()
//	}
}
//用户查询按钮
function _w_bquery(){
	var val = $('#caq_userName').val();
	var config ="";
	if(val != ""){
		config =  '1-O_NAME-'+val+'-inc|+';
	}
	$.ajax({
		type:'post',
		url:_m_url+'framework/loadDataQ?dsId=ds_bs_operators&rows=1000',
		data:{'config':config},
		success:function(data){
			console.log(data);
			$('#caq_USERLIST').datagrid('loadData',data);
			$('#caq_userName').val('');
		}
	})
}
//收缩的checkbox
function shouSuo(obj){
	if($(obj).attr('checked')=='checked'){
		$($(obj).parents('table')[0]).find("tr[gn='T']").map(function(){
			$(this).hide();
		});
	}else{
		$($(obj).parents('table')[0]).find("tr[gn='T']").map(function(){
			$(this).show();
		});
	}
}
//收缩的字
function shouSuoIn(obj){
	var prevObj = $(obj).prev();
	if($(prevObj).attr('checked')=='checked'){
		$(prevObj).attr('checked',false);
	}else{
		$(prevObj).attr('checked','checked');
	}
	shouSuo(prevObj);
}
//搜索模块中的搜索功能
function searchMGB(obj,num){
	$('#searchDiv').html('');
	var selRows = $('#caq_USERLIST').datagrid('getSelected');
	var oid = "";
	if(selRows != null){
		oid= selRows.O_ID; 
	}
	var valObj;
	if(num == 0){ 																					//按钮
		valObj = $($($(obj).parents('td').prev()).find('input')[0]).val();
	}else if(num == 1){  																			//回车
		valObj = $(obj).val();
	}
	$.ajax({
		type:'post',
		url:_m_url+'menu/searchPerms',
		data:{'valStr':valObj,'oid':oid},
		success:function(data){
			if(data != null){
				//模糊查询出的整体数据
				var moList = data.modelList;
				var gnList = data.dataStroeList;
				var butnList = data.butnList;
				var pageTable="";
				pageTable="<table><tr><td id='findUserPerms'>";
				if(moList != null){
					//拼接界面
					for(var i = 0; i<moList.length;i++){
						pageTable+="<div style='display:inline-block;zoom:1;text-align: left;padding-top:10px;'> "+
										" <div id='d_uid_24' style='width:800px;'>"+
											"<div class='caption-bar'> "+
												"<div class='collapse-button' onclick='closeP(this)'></div>"+
												"<div class='caption'>"+moList[i].MENU_NAME+"</div>" +
												"<div class='bar-right'></div>"+
											"</div>"+
											"<div class='body' style='position: relative;' >"+
												"<div class='d-container-ui-default' style='overflow: hidden; position: relative; text-align: left;'>"+
													"<table style='border-collapse:collapse; width: 800px;'>"+
														"<tr>"+
															"<td style='width: 100px;'>"+
																"<div style='float: left;'>"+
																 	"<input type='checkbox' mid='"+moList[i].MENU_ID+"' haveFlag='0'  onclick='modChange(this)' /><span onclick='checkboxUSel(this,0)' style='cursor: pointer;'>"+moList[i].MENU_NAME+":</span>"+
																 "</div>"+
															"</td>"+
															"<td  style='border-bottom: 1px solid #8db2e3;width: 150px; '>"+ 
																"<div style='float: left;'>"+
															 		"<input type='checkbox'  onclick='chengeAllFuncU(this)' /><span onclick='checkboxUSel(this,1)' style='cursor: pointer;'>全选/全取消</span>"+
															    "</div>"+
															"</td>"+
															"<td>   </td>"+
														"</tr>";
 
						if(gnList!= null){
							for(var j=0;j<gnList.length;j++){
								if(gnList[j].MODUAL_ID == moList[i].MENU_ID){
									pageTable+="<tr gn='T'> "+
									"<td>   </td>"+
									"<td style=' border-left: 1px solid #8db2e3;border-right: 1px solid #8db2e3; border-bottom: 1px solid #8db2e3;'>"+
										"<div style='float: left;'>"+
										"<input type='checkbox' funcId='"+gnList[j].FUNC_ID+"' modId='"+gnList[j].MODUAL_ID+"'  haveFlag='0'  onclick='gnChange(this)' /><span onclick='checkboxUSel(this,2)' style='cursor: pointer;'>"+gnList[j].FUNC_NAME+":</span>"+
										"</div>"+
									"</td>";
									if(butnList!= null){
										for(var k=0;k<butnList.length;k++){
											if(butnList[k].FUNC_ID == gnList[j].FUNC_ID){
												pageTable+="<td style='border-bottom: 1px solid #8db2e3;'>" +
												"<div style='float: left;'>"+
												"<input type='checkbox' onclick='butnChange(this)'  haveFlag='0'   butnId='"+butnList[k].BUTN_ID+"' butnCode='"+butnList[k].BUTN_CODE+"' /><span onclick='checkboxUSel(this,3)' style='cursor: pointer;'>"+butnList[k].BUTN_NAME+"</span>"+
												"</div></td>";
											}
										}
									}
									pageTable+="</tr>";
								} 
							}
						} 
						pageTable+="</table></div></div></div></div>";
					}	
				} 
				pageTable+="</td></tr></table>";
				$('#searchDiv').append(pageTable);
				$.parser.parse('#searchDiv');
				//根据用户Id查询出用户拥有的权限
				if(oid != ""){
					 var haveModual = data.haveModual; 																		      //已有的模块
					 var glist = data.haveFuncs; 																		     	  //已有的功能
					 var blist = data.haveButns;
					 selcFunc('findUserPerms',haveModual,glist,blist);
				}
			}
		}
	})
}
//保存搜索中的权限
function saveSearchPerms(){
	var selRows = $('#caq_USERLIST').datagrid('getSelected');
	if(selRows == null){
		$.messager.alert("提示","员工没有选择，请选择员工之后再添加！");
		return;
	}
	var oid= selRows.O_ID; 	
	var allList = {};
	var mList = new Array();																					   //获取到所有选中模块的模块Id
	var gList = new Array();
	var bList = new Array();																				       //获取到所有选中模块的模块Id
	var haveFlag="";
	$('#findUserPerms').find('table').find('input[mid]').map(function(){
		haveFlag = $(this).attr('haveFlag');
		var modualList = new Array();
		if($(this).attr('checked')=="checked"){
			if(haveFlag=='0'){
				modualList[0] = $(this).attr('mid');
				modualList[1] = '0';
				modualList[2] = '1';
				mList.push(modualList);
			}
		}else{
			if(haveFlag=='1'){
				modualList[0] = $(this).attr('mid');
				modualList[1] = '1';
				modualList[2] = '0';
				mList.push(modualList);
			}
		}
	});
	$('#findUserPerms').find('table').find('input[funcId]').map(function(){
		haveFlag = $(this).attr('haveFlag');
		var modId = $($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('mid');					//模块Id
		var gmfList = new Array();
		if($(this).attr('checked')=="checked"){
			if(haveFlag=='0'){
				gmfList[0]=modId;
				gmfList[1]=$(this).attr('funcId');																		//功能ID
				gmfList[2] = '0';
				gmfList[3] = '1';
				gList.push(gmfList);
			}
		}else{
			if(haveFlag=='1'){
				gmfList[0] = modId;
				gmfList[1] = $(this).attr('funcId');																		//功能ID
				gmfList[2] = '1';
				gmfList[3] = '0';
				gList.push(gmfList);
			}
		}
	});
	$('#findUserPerms').find('table').find('input[butnCode]').map(function(){
		haveFlag = $(this).attr('haveFlag');
		var gmfbList = new Array();
		var modId = $($($($(this).parents('table')[0]).find('tr')[0]).find('input')[0]).attr('mid'); 			 //模块Id
		var funcId = $($(this).parents('td').prev().find('input')[0]).attr('funcId');                 			 //功能Id
		if($(this).attr('checked')=="checked"){
			if(haveFlag=='0'){
				gmfbList[0]=modId;
				gmfbList[1]=funcId;
				gmfbList[2]=$(this).attr('butnId');
				gmfbList[3]=$(this).attr('butnCode');
				gmfbList[4]='0';
				gmfbList[5]='1';
				bList.push(gmfbList);
			}
		}else{
			if(haveFlag=='1'){
				gmfbList[0]=modId;
				gmfbList[1]=funcId;
				gmfbList[2]=$(this).attr('butnId');
				gmfbList[3]=$(this).attr('butnCode');
				gmfbList[4]='1';
				gmfbList[5]='0';
				bList.push(gmfbList);
			}
		}
	});
	allList.oList = mList;
	allList.gList = gList;
	allList.bList = bList;
	allList.flagStr = '3';
	$.ajax({
		type:'post',
		url:_m_url+'menu/saveSearchPerms?oid='+oid,
		contentType: "application/json;charset=utf-8",
		dataType: "json",
		data:JSON.stringify(allList),
		success:function(data){
			if(data){
				$.messager.alert('提示',"保存成功！");
			}else{
				$.messager.alert('提示','保存失败！');
			}
		}
	})
}
